<?php

/**
 * 
 * @desc 数据库连接实例类
 * @author keluo <pycorvn@yeah.net>
 * @since 2012-4-25 15:08:13
 */
class SCDatabase {
	private static $_instances = array();
	
	/**
	 * 返回数据库连接实例
	 * @param string $dsn
	 */
	public static function get($dsn) {
		is_null($dsn) && ($dsn = SCConfig::get('DB_CONNECT'));
		if(!isset(self::$_instances[$dsn])) {
			$config = SCConfig::getDB($dsn);
			self::$_instances[$dsn] = mysql_connect($config['db_host'], $config['db_user'], 
					$config['db_pwd'], true); // 第四个参数必选设置为true，否则切换数据库会出错
			mysql_select_db($config['db_name'], self::$_instances[$dsn]);
			mysql_query('SET NAMES utf8', self::$_instances[$dsn]);
		}
		
		return self::$_instances[$dsn];
	}
}